package com.mall.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.mall.bean.Blog;
import com.mall.bean.Goods;
import com.mall.bean.Search;
import com.mall.bean.User;
import com.mall.bean.view.UserView;
import com.mall.service.IBlogService;
import com.mall.service.ICartService;
import com.mall.service.IGoodsService;
import com.mall.service.IKeywordService;
import com.mall.service.ISearchService;
import com.mall.service.IUserService;


@Controller
public class UserController {

	@Resource
	private IUserService userService;

	@Resource
	private IKeywordService keywordService;

	@Resource
	private ISearchService searchService;

	@Resource
	private IGoodsService goodsService;

	@Resource
	private IBlogService blogService;

	@Resource
	private ICartService cartService;

	// 注册用户

	@RequestMapping(value = "addUser", produces = "application/json")
	public @ResponseBody
	Map<String, Object> register1(@ModelAttribute UserView userView, HttpSession session) {

		Map<String, Object> res = new HashMap<String, Object>();
		String success = "恭喜您注册成功";
		String incorrect = "您的密码前后不统一";
		String exist = "您的用户或密码已存在";
		Date time = new Date();
		SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String date = ft.format(time).split(" ")[0];
		String phone = userView.getPhone();
		String email = userView.getEmail();
		System.out.println("邮箱1：" + email);
		int check = userService.checkUser(phone);
		int check2 = userService.checkUser2(email);
		System.out.println("邮箱2：" + email);
		if (check == 0 && check2 == 0) {
			userView.setDate(date);
			userView.setHeadImage("hdfs://iflytek002:9000/photo/发风格-2021-06-29-230534-50419924.jpeg");
			String staus = userService.register(userView);
			if ("success".equals(staus)) { //return "success";//登录页

				res.put("success", success);
				return res;
			} else {
				res.put("incorrect", incorrect);
				System.out.println("您的密码前后不统一");
				return res;
			}
		} else {
			res.put("exist", exist);
			System.out.println("您的邮箱或手机号已注册");
			return res;
		}
	}

	// 登录用户
	@RequestMapping("index-2")
	public String login(@Param("email_phone") String email_phone, @Param("password") String password, HttpSession session, Model model, Search search, Goods goods, Blog blog) {

		/*      正则表达式判断是否输入正确
		 * String phoneType = "^(13|14|15|17|18)[0-9]{9}$"; String
		 * emailType="[\\w-]+(\\.[\\w-]+)*@([\\w-]+\\.)+\\w{2,14}";
		 * System.out.println(email_phone); Pattern r = Pattern.compile(phoneType);
		 * Pattern r1 = Pattern.compile(emailType); boolean p =
		 * r.matches(phoneType,email_phone); boolean e= r1.matches(emailType,
		 * email_phone);
		 */

		User user1 = userService.login(email_phone, password);
		User user2 = userService.login1(email_phone, password);
		System.out.println("电话：" + user1);
		System.out.println("邮箱：" + user2);

		User user = userInfo(session);

		//热搜
		List<String> key = keywordService.findKeyword();
		search.setGoods_name(key.get(0));
		List<Search> key1 = searchService.findKeyword(search);
		model.addAttribute("key1", key1);

		search.setGoods_name(key.get(1));
		List<Search> key2 = searchService.findKeyword(search);
		model.addAttribute("key2", key2);

		search.setGoods_name(key.get(2));
		List<Search> key3 = searchService.findKeyword(search);
		model.addAttribute("key3", key3);

		search.setGoods_name(key.get(3));
		List<Search> key4 = searchService.findKeyword(search);
		model.addAttribute("key4", key4);

		search.setGoods_name(key.get(4));
		List<Search> key5 = searchService.findKeyword(search);
		model.addAttribute("key5", key5);

		search.setGoods_name(key.get(5));
		List<Search> key6 = searchService.findKeyword(search);
		model.addAttribute("key6", key6);

		search.setGoods_name(key.get(6));
		List<Search> key7 = searchService.findKeyword(search);
		model.addAttribute("key7", key7);

		search.setGoods_name(key.get(7));
		List<Search> key8 = searchService.findKeyword(search);
		model.addAttribute("key8", key8);

		//闲置商品
		List<Goods> goodsAll = goodsService.findGoods(goods);
		model.addAttribute("goodsAll", goodsAll);

		//热搜列表展示
		model.addAttribute("key", key);

		System.out.println("session：" + user);

		//展示博客
		List<Blog> allBlog = blogService.findAllBlog(blog);
		model.addAttribute("allBlog", allBlog);
		if (user1 != null) {
			session.setAttribute("user", user1);
			//购物车悬浮
			User user3 = cartService.myCart(user);
			model.addAttribute("user", user3);


			return "index-2";
		} else if (user2 != null) {
			session.setAttribute("user", user2);
			//购物车悬浮
			User user4 = cartService.myCart(user);
			model.addAttribute("user", user4);
			return "index-2";
		} else if (user != null) {
			return "index-2";
		} else {
			return "login";
		}
	}

	//退出登录
	@RequestMapping("drop")
	public String drop(Search search, Model model, Goods goods, HttpSession session) {
		//热搜
		List<String> key = keywordService.findKeyword();
		search.setGoods_name(key.get(0));
		List<Search> key1 = searchService.findKeyword(search);
		model.addAttribute("key1", key1);

		search.setGoods_name(key.get(1));
		List<Search> key2 = searchService.findKeyword(search);
		model.addAttribute("key2", key2);

		search.setGoods_name(key.get(2));
		List<Search> key3 = searchService.findKeyword(search);
		model.addAttribute("key3", key3);

		search.setGoods_name(key.get(3));
		List<Search> key4 = searchService.findKeyword(search);
		model.addAttribute("key4", key4);

		search.setGoods_name(key.get(4));
		List<Search> key5 = searchService.findKeyword(search);
		model.addAttribute("key5", key5);

		search.setGoods_name(key.get(5));
		List<Search> key6 = searchService.findKeyword(search);
		model.addAttribute("key6", key6);

		search.setGoods_name(key.get(6));
		List<Search> key7 = searchService.findKeyword(search);
		model.addAttribute("key7", key7);

		search.setGoods_name(key.get(7));
		List<Search> key8 = searchService.findKeyword(search);
		model.addAttribute("key8", key8);

		//闲置商品
		List<Goods> goodsAll = goodsService.findGoods(goods);
		model.addAttribute("goodsAll", goodsAll);

		//热搜列表展示
		model.addAttribute("key", key);
		session.invalidate();
		return "index-1";
	}

	//切换账号
	@RequestMapping("change")
	public String change(HttpSession session) {

		session.invalidate();
		return "login";
	}

	//获取用户信息
	public static User userInfo(HttpSession session) {

		User user = (User) session.getAttribute("user");
		return user;
	}


}